## 
## Copyright (C) 2008+ Spark Zheng
##
## dlib/test/Makefile
## Generate the unit test program.  
##

PREFIX		?= /home/sparkzheng/tools
PROJECT_HOME	?= /home/sparkzheng/Projects/mfs_projects/dlib

CODE_TYPE       ?= c
CODE_VERSION    ?= alpha

export PREFIX
export PROJECT_HOME
export CODE_TYPE
export CODE_VERSION

CC			= gcc
CXX			= g++

CFLAGS		= -g -W -Wall -fPIC
CXXFLAGS	= -g -W -Wall -fPIC

CFLAGS_WARN	= -Wshadow -Wcast-qual -Wunreachable-code -Wredundant-decls \
			  -Wmissing-prototypes -Wstrict-prototypes -Wnested-externs -Winline
CXXFLAGS_WARN	= -Winline -Wunreachable-code -Winline -Wunreachable-code

CFLAGS_DEBUG	:= -D_DEBUG $(CFLAGS_WARN)
CXXFLAGS_DEBUG	:= -D_DEBUG $(CXXFLAGS_WARN)

CFLAGS_OPT		= -O2 -fomit-frame-pointer
CXXFLAGS_OPT	= -O2 -fomit-frame-pointer

INCFLAGS	:= -I. -I./include -I.. -I../include -I$(PREFIX)/include \
			   -I$(PROJECT_HOME) -I$(PROJECT_HOME)/include

LDFLAGS 	:= -Wl,-rpath,$(PROJECT_HOME) \
			   -Wl,-rpath,$(PROJECT_HOME)/src\
			   -Wl,-rpath,$(PREFIX)/lib \
			   -L$(PROJECT_HOME) -L$(PROJECT_HOME)/src \
			   -L. -L/usr/local/lib -L$(PREFIX)/lib
LIB_LDFLAGS	:= $(LDFLAGS) -Wl,-E -shared

LIBS 		= -lpthread

RANLIB		= ranlib
AR			= ar
ARFLAGS		= ru

LD			= ld

INSTALL 	?= install -c
UNINSTALL	?= rm -f

DIRS		= 


TARGETS 	= $(EXEC_TARGETS) $(LIB_TARGETS)
EXEC_TARGETS	= $(LOG_TARGETS) \
		  test_except \
		  test_random \
		  test_misc \
		  test_str \
		  test_hash \
		  test_conf \
		  test_confloose \
		  test_bit \
		  test_list \
		  test_josephus \
		  test_rbtree \
		  test_seqtree \
		  test_sptree \
		  test_sbtree \
		  $(HTABLE_TARGETS) \
		  $(MEM_TARGETS) \
		  test_buffer \
		  $(TPOOL_TARGETS) \
		  $(SERIALIZE) \
		  $(HEAP_TARGETS)
		  
LIB_TARGETS	= 
LIB_HEADS	= 
		  
LOG_TARGETS	= test_log_nosyslog \
			  test_log_syslog
HTABLE_TARGETS	= test_htable_nomarena \
				  test_htable_marena \
				  test_htable_open_nomarena \
				  test_htable_open_marena
				  
MEM_TARGETS	= test_mempool \
			  test_mempool_factor \
			  test_mempool_rbtree 

TPOOL_TARGETS = test_tpool \
				test_tpool_enhance

SERIALIZE	= test_phpserialize

HEAP_TARGETS = test_sheap

OBJECTS		= $(EXEC_OBJECTS) $(LIB_OBJECTS)
EXEC_OBJECTS	= test_log.o ../src/log.o \
		  test_log_syslog.o ../src/log_syslog.o \
		  test_except.o ../src/except.o \
		  test_random.o ../src/random.o \
		  test_misc.o ../src/misc.o \
		  test_str.o ../src/str.o \
		  test_hash.o ../src/hash.o \
		    ../src/htable.o ../src/mempool.o \
		  test_conf.o ../src/conf.o ../src/htable.o ../src/hash.o \
			../src/mempool.o ../src/str.o \
		  test_confloose.o ../src/confloose.o ../src/mempool.o \
		  	../src/str.o \
		  test_bit.o ../src/bit.o \
		  test_list.o \
		  test_josephus.o \
		  test_rbtree.o ../src/tree.o \
		  test_seqtree.o ../src/seqtree.o \
		  test_sptree.o ../src/tree.o \
		  test_sbtree.o ../src/tree.o \
		  test_htable.o ../src/htable.o \
		  	../src/hash.o ../src/mempool.o \
		  test_htable.o ../src/htable_marena.o \
		  	../src/hash.o ../src/mempool.o \
		  test_htable_open.o ../src/htable.o \
		  	../src/hash.o ../src/mempool.o \
		  test_htable_open.o ../src/htable_marena.o \
		  	../src/hash.o ../src/mempool.o \
		  test_mempool.o ../src/mempool.o ../src/random.o \
		  	../src/threadpool.o \
		  test_mempool_factor.o ../src/mempool_factor.o ../src/random.o \
		  	../src/threadpool.o \
		  test_mempool.o ../src/mempool_rbtree.o ../src/random.o \
		  	../src/threadpool.o \
		  test_buffer.o ../src/buffer.o ../src/mempool.o \
		  test_tpool.o ../src/threadpool.o ../src/mempool.o \
		  test_tpool_enhance.o ../src/threadpool.o ../src/mempool.o \
		    ../src/buffer.o \
		  test_phpserialize.o ../src/serialize.o ../src/mempool.o \
		  	../src/htable.o ../src/hash.o \
		  test_sheap.o ../src/heap.o ../src/mempool.o
		  
LIB_OBJECTS	= 


all: $(TARGETS)

test_log: $(LOG_TARGETS)
test_log_nosyslog: test_log.o ../src/log.o
	$(CC) -g -pg -o $@ test_log.o ../src/log.o $(LDFLAGS) $(LIBS)
test_log_syslog.o: test_log.c
	$(CC) -g -pg -o $@ -c $< $(CFLAGS) $(CFLAGS_DEBUG) $(INCFLAGS) \
		-D_DLIB_LOG_USE_SYSLOG
../src/log_syslog.o: ../src/log.c
	$(CC) -g -pg -o $@ -c $< $(CFLAGS) $(CFLAGS_DEBUG) $(INCFLAGS) \
		-D_DLIB_LOG_USE_SYSLOG
test_log_syslog: test_log_syslog.o ../src/log_syslog.o
	$(CC) -g -pg -o $@ test_log_syslog.o ../src/log_syslog.o $(LDFLAGS) $(LIBS)
.PHONY: test_log


test_except: test_except.o ../src/except.o
	$(CC) -g -pg -o $@ test_except.o ../src/except.o $(LDFLAGS) $(LIBS)

test_random: test_random.o ../src/random.o
	$(CC) -g -pg -o $@ test_random.o ../src/random.o $(LDFLAGS) $(LIBS)

test_misc: test_misc.o ../src/misc.o
	$(CC) -g -pg -o $@ test_misc.o ../src/misc.o $(LDFLAGS) $(LIBS)
	
test_str: test_str.o ../src/str.o
	$(CC) -g -pg -o $@ test_str.o ../src/str.o $(LDFLAGS) $(LIBS)

test_hash: test_hash.o ../src/hash.o ../src/htable.o ../src/mempool.o
	$(CC) -g -pg -o $@ test_hash.o ../src/hash.o ../src/htable.o ../src/mempool.o \
		$(LDFLAGS) $(LIBS)
	
test_conf: test_conf.o ../src/conf.o ../src/htable.o ../src/hash.o \
	   ../src/mempool.o ../src/str.o
	$(CC) -g -pg -o $@ test_conf.o ../src/conf.o ../src/htable.o \
		../src/hash.o ../src/mempool.o ../src/str.o $(LDFLAGS) $(LIBS)

test_confloose: test_confloose.o ../src/confloose.o ../src/mempool.o ../src/str.o
	$(CC) -g -pg -o $@ test_confloose.o ../src/confloose.o ../src/mempool.o \
		../src/str.o $(LDFLAGS) $(LIBS)

test_bit: test_bit.o ../src/bit.o
	$(CC) -g -pg -o $@ test_bit.o ../src/bit.o $(LDFLAGS) $(LIBS)


test_list: test_list.o
	$(CC) -g -pg -o $@ test_list.o $(LDFLAGS) $(LIBS)
test_josephus: test_josephus.o
	$(CC) -g -pg -o $@ test_josephus.o $(LDFLAGS) $(LIBS)

test_rbtree: test_rbtree.o ../src/tree.o
	$(CC) -g -pg -o $@ test_rbtree.o ../src/tree.o $(LDFLAGS) $(LIBS)

../src/seqtree.o: ../src/tree.c
	$(CC) -g -pg -o $@ -c $< $(CFLAGS) $(CFLAGS_DEBUG) $(INCFLAGS) \
		-D_DLIB_RBTREE_SIZE
test_seqtree.o: test_seqtree.c
	$(CC) -g -pg -o $@ -c $< $(CFLAGS) $(CFLAGS_DEBUG) $(INCFLAGS) \
		-D_DLIB_RBTREE_SIZE
test_seqtree: test_seqtree.o ../src/seqtree.o
	$(CC) -g -pg -o $@ test_seqtree.o ../src/seqtree.o $(LDFLAGS) $(LIBS)

test_sptree: test_sptree.o ../src/tree.o
	$(CC) -g -pg -o $@ test_sptree.o ../src/tree.o $(LDFLAGS) $(LIBS)

test_sbtree: test_sbtree.o ../src/tree.o
	$(CC) -g -pg -o $@ test_sbtree.o ../src/tree.o $(LDFLAGS) $(LIBS)
	

test_htable: $(HTABLE_TARGETS)
test_htable_nomarena: test_htable.o ../src/htable.o \
		      ../src/hash.o ../src/mempool.o
	$(CC) -g -pg -o $@ test_htable.o ../src/htable.o ../src/hash.o \
		../src/mempool.o $(LDFLAGS) $(LIBS)
../src/htable_marena.o: ../src/htable.c
	$(CC) -g -pg -o $@ -c $< $(CFLAGS) $(CFLAGS_DEBUG) $(INCFLAGS) \
		-D_DLIB_HTABLE_USE_MARENA
test_htable_marena: test_htable.o ../src/htable_marena.o \
		    ../src/hash.o ../src/mempool.o
	$(CC) -g -pg -o $@ test_htable.o ../src/htable_marena.o \
		../src/hash.o ../src/mempool.o $(LDFLAGS) $(LIBS)

test_htable_open_nomarena: test_htable_open.o ../src/htable.o \
		      ../src/hash.o ../src/mempool.o
	$(CC) -g -pg -o $@ test_htable_open.o ../src/htable.o ../src/hash.o \
		../src/mempool.o $(LDFLAGS) $(LIBS)
test_htable_open_marena: test_htable_open.o ../src/htable_marena.o \
		    ../src/hash.o ../src/mempool.o
	$(CC) -g -pg -o $@ test_htable_open.o ../src/htable_marena.o \
		../src/hash.o ../src/mempool.o $(LDFLAGS) $(LIBS)
.PHONY: test_htable


test_mpool: $(MEM_TARGETS)
test_mempool: test_mempool.o ../src/mempool.o ../src/random.o ../src/threadpool.o
	$(CC) -g -pg -o $@ test_mempool.o ../src/mempool.o ../src/random.o \
		../src/threadpool.o $(LDFLAGS) $(LIBS)
test_mempool_factor.o: test_mempool.c
	$(CC) -g -pg -o $@ -c $< $(CFLAGS) $(CFLAGS_DEBUG) $(INCFLAGS) \
		-D_DLIB_MPOOL_USE_FACTOR
../src/mempool_factor.o: ../src/mempool.c
	$(CC) -g -pg -o $@ -c $< $(CFLAGS) $(CFLAGS_DEBUG) $(INCFLAGS) \
		-D_DLIB_MPOOL_USE_FACTOR
test_mempool_factor: test_mempool_factor.o ../src/mempool_factor.o \
		     ../src/random.o ../src/threadpool.o
	$(CC) -g -pg -o $@ test_mempool_factor.o ../src/mempool_factor.o \
		../src/random.o ../src/threadpool.o $(LDFLAGS) $(LIBS)
../src/mempool_rbtree.o: ../src/mempool.c
	$(CC)  -o $@ -c $< $(CFLAGS) $(CFLAGS_DEBUG) $(INCFLAGS) \
		-D_DLIB_MPOOL_USE_RBTREE
test_mempool_rbtree: test_mempool.o ../src/mempool_rbtree.o ../src/random.o \
		     ../src/threadpool.o ../src/tree.o
	$(CC) -g -pg -o $@ test_mempool.o ../src/mempool_rbtree.o ../src/random.o \
		../src/threadpool.o ../src/tree.o $(LDFLAGS) $(LIBS)
.PHONY: test_mem


test_buffer: test_buffer.o ../src/buffer.o ../src/mempool.o
	$(CC) -g -pg -o $@ test_buffer.o ../src/buffer.o ../src/mempool.o \
		$(LDFLAGS) $(LIBS)


test_tp: $(TPOOL_TARGETS)
test_tpool: test_tpool.o ../src/threadpool.o ../src/mempool.o
	$(CC) -g -pg -o $@ test_tpool.o ../src/threadpool.o ../src/mempool.o \
		$(LDFLAGS) $(LIBS)

test_tpool_enhance: test_tpool_enhance.o ../src/threadpool.o ../src/mempool.o \
			../src/buffer.o
	$(CC) -g -pg -o $@ test_tpool_enhance.o ../src/threadpool.o ../src/mempool.o \
		../src/buffer.o $(LDFLAGS) $(LIBS)
.PHONY: test_tp

test_serialize: $(SERIALIZE)
test_phpserialize: test_phpserialize.o ../src/serialize.o ../src/mempool.o \
			../src/htable.o ../src/hash.o
	$(CC) -g -pg -o $@ test_phpserialize.o ../src/serialize.o ../src/mempool.o \
		../src/htable.o ../src/hash.o $(LDFLAGS) $(LIBS)
.PHONY: test_serialize

test_heap: $(HEAP_TARGETS)
test_sheap: test_sheap.o ../src/heap.o ../src/mempool.o
	$(CC) -g -pg -o $@ test_sheap.o ../src/heap.o ../src/mempool.o $(LDFLAGS) $(LIBS)
.PHONY: test_heap

.SUFFIXES:
.SUFFIXES:	.c .cpp .o

ifneq ("$(CODE_VERSION)","release")
.c.o :
	$(CC) -o $@ -c $< $(CFLAGS) $(CFLAGS_DEBUG) $(INCFLAGS)
.cpp.o :
	$(CXX) -o $@ -c $< $(CXXFLAGS) $(CXXFLAGS_DEBUG) $(INCFLAGS)
else
.c.o :
	$(CC) -o $@ -c $< $(CFLAGS) $(CFLAGS_OPT) $(INCFLAGS)
.cpp.o :
	$(CXX) -o $@ -c $< $(CXXFLAGS) $(CXXFLAGS_OPT) $(INCFLAGS)
endif


count:
	-wc *.c *.cc *.C *.cpp *.h *.hpp 2>/dev/null
	set -e; for i in $(DIRS); do \
		echo "make count enter $$i."; \
		make -C $$i count; \
		echo "make count leave $$i."; \
	done;

clean:
	-rm -f $(OBJECTS) *.swp *~ *.out core.*
	set -e; for i in $(DIRS); do \
		echo "make clean enter $$i."; \
		make -C $$i clean; \
		echo "make clean leave $$i."; \
	done;

cleanall:
	-rm -f $(OBJECTS) *.swp *~ *.out core.* $(TARGETS)
	set -e; for i in $(DIRS); do \
		echo "make clean enter $$i."; \
		make -C $$i clean; \
		echo "make clean leave $$i."; \
	done;


.PHONY: all
.PHONY: count
.PHONY: clean
.PHONY: cleanall
